#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "los_task.h"

uint32_t task1_id, task2_id, sec1, sec2;
uint32_t task1_entry()
{
    while(1)
    {
        printf("task1 running\n");
        sec1++;
        if(sec1%3 == 0){
            LOS_TaskResume(task2_id);
            LOS_TaskSuspend(task1_id);
        }
        LOS_TaskDelay(1000);
    }  
}

uint32_t task2_entry()
{
    while(1)
    {
        printf("task2 running\n");
        sec2++;
        if(sec2%3 == 0){
            LOS_TaskResume(task1_id);
            LOS_TaskSuspend(task2_id);
        }
        LOS_TaskDelay(1000);
    }  
}

uint32_t task_entry()
{
    uint32_t ret;
    TSK_INIT_PARAM_S param;
    LOS_TaskLock();

    param.pcName = "task1";
    param.pfnTaskEntry = task1_entry;
    param.usTaskPrio = 10;
    param.uwStackSize = 4*0x400;
    param.stackAddr = 0;
    ret = LOS_TaskCreate(&task1_id, &param);
    if(ret != LOS_OK){
        printf("create task1 failed, errno = 0x%x\n", ret);
        goto exit_entry;
    }

    param.pcName = "task2";
    param.pfnTaskEntry = task2_entry;
    param.usTaskPrio = 10;
    param.uwStackSize = 0x400*4;
    param.stackAddr = 0;
    ret = LOS_TaskCreateOnly(&task2_id, &param);
    if(ret != LOS_OK){
        printf("create task2 failed, errno = 0x%x\n", ret);
        goto exit_entry;
    }

exit_entry:
    LOS_TaskUnlock();
    return ret;
}
